import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/home";

Vue.use(VueRouter); //告诉vue使用vuerouter

const router = new VueRouter({
	routes: [
		{ path: "/", redirect: "/home" },
		{
			path: "/register",
			component: () => import("@/views/register"),
			meta: { showFooter: false },
		},
		{
			path: "/login",
			component: () => import("@/views/login"),
			meta: { showFooter: false },
		},
		{
			path: "/home",
			component: () => import("@/views/home"),
			meta: { showFooter: true },
		},
		{
			path: "/detail/:id",
			component: () => import("@/views/detail"),
			meta: { showFooter: false, title: "详情页" },
		},
		{
			path: "/fenlei",
			component: () => import("@/views/fenlei"),
			meta: { showFooter: true },
		},
		{
			path: "/cart",
			component: () => import("@/views/cart"),
			meta: { showFooter: true, title: "购物车" },
		},
		{
			path: "/address",
			component: () => import("@/views/address"),
			meta: { showFooter: true, title: "地址信息" },
		},
		{
			path: "/addressList",
			component: () => import("@/views/addressList"),
			meta: { showFooter: true, title: "地址列表" },
		},
		{
			path: "/addressDetail",
			component: () => import("@/views/addressDetail"),
			meta: { showFooter: true, title: "地址详情" },
		},
		{
			path: "/order",
			component: () => import("@/views/order"),
			meta: { showFooter: true, title: "订单页面" },
		},
		{
			path: "/orderDetail/:id",
			component: () => import("@/views/orderDetail"),
			meta: { showFooter: true, title: "订单详情" },
		},
		{
			path: "/mine",
			component: () => import("@/views/mine"),
			meta: { showFooter: true },
			beforeEnter: (to, from, next) => {
				// 独享守卫，比如从cart到mine
				// console.log(to); //指的就是mine
				// console.log(from); //指的就是cart
				// 判断有没有登录
				const isLogin = localStorage.getItem("token");
				if (!isLogin) {
					// 说明没有登陆,让他跳转到登录页面
					next("/login");
				} else {
					// 说明登陆了,就放行
					next();
				}
			},
		},
	],
});

export default router;
